import requests
from common.sql_util import DBUtil

class IHRMCURDAPI():
    # 添加员工
    @classmethod
    def add_emp(cls, token, json_data):
        url = "http://ihrm2-test.itheima.net/api/sys/user"
        header = {"Content-Type": "application/json", "Authorization": token}
        res = requests.post(url=url, headers=header, json=json_data)
        return res

    # 查询员工
    @classmethod
    def query_emp(cls, emp_id, token):
        url = f"http://ihrm2-test.itheima.net/api/sys/user/{emp_id}"
        head = {"Authorization": token}
        res = requests.get(url=url, headers=head)
        return res

    # 修改员工
    @classmethod
    def modify_emp(cls, emp_id, token, json_data):
        url = "http://ihrm2-test.itheima.net/api/sys/user/" + emp_id
        header = {"Content-Type": "application/json", "Authorization": token}
        res = requests.put(url=url, headers=header, json=json_data)
        return res

    # 删除员工
    @classmethod
    def del_emp(cls, emp_id, token):
        url = f"http://ihrm2-test.itheima.net/api/sys/user/{emp_id}"
        head = {"Authorization": token}
        res = requests.delete(url=url, headers=head)
        return res


if __name__ == '__main__':
    token = "Bearer 06da98bf-3ef7-47ac-a8e8-c7865d5afd2e"
    emp_id = "1571044032300199936"
    json_data = {"username": "路明泽",
                 "mobile": "13985546547",
                 "workNumber": "9559"
                 }
    resp = IHRMCURDAPI.add_emp(token, json_data)
    print(resp.json())
    resp = IHRMCURDAPI.query_emp(emp_id, token)
    print(resp.json())
    # json_data = {"departmentId": "1066239913642561537"}
    # resp = IHRMCURDAPI.modify_emp(emp_id, token, json_data)
    # print(resp.json())
    # resp = IHRMCURDAPI.del_emp(emp_id, token)
    # print(resp.json())
    DBUtil.chg_db("ihrm")
    DBUtil.select_one(f"select is_delete from bs_user where id ={emp_id};")